package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IFreelanceSkillDAO;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.FreelanceSkill;
import org.insia.smartposse.metier.Skill;
import org.insia.smartposse.metier.webservice.LightFreelanceSkill;

/**
 * DAO for FreelanceSkill class based on GenericDAO.
 * @author sok hout
 *
 */
public class FreelanceSkillDAO 
extends GenericDAO<FreelanceSkill, Long> 
implements IFreelanceSkillDAO {
	/**
	 * Empty constructor.
	 *
	 */
	public FreelanceSkillDAO() {
		type = FreelanceSkill.class;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceSkillDAO#getFreelanceFromSkill(long)
	 */
	@SuppressWarnings("unchecked")
	public List<Freelance> getFreelanceFromSkill(long skill_id) {
		List<Freelance> rst = null;
		Skill skill = null;
		try {
			skill = DAOFactory.getSkillDAO().get(skill_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rst = (List<Freelance>)_SESSION.createQuery(
				"select freelance from FreelanceSkill as fskill where fskill.skill = ?"
		).setEntity(0, skill).list();
		
		return rst;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceSkillDAO#createLightFreelanceSkill(org.insia.smartposse.metier.webservice.LightFreelanceSkill)
	 */
	public LightFreelanceSkill createLightFreelanceSkill(LightFreelanceSkill lskill) {
		try {
			FreelanceSkill fskill = new FreelanceSkill();
			fskill.setFreelance(DAOFactory.getFreelanceDAO().get(lskill.getFreelance()));
			fskill.setPreference(lskill.getPreference());
			fskill.setRate(lskill.getRate());
			fskill.setSkill(DAOFactory.getSkillDAO().get(lskill.getSkill()));
			lskill.setId(DAOFactory.getFreelanceSkillDAO().create(fskill));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lskill;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceSkillDAO#createLightFreelanceSkill(java.util.List)
	 */
	public List<LightFreelanceSkill> createLightFreelanceSkill(List<LightFreelanceSkill> llfskill){
		List<LightFreelanceSkill> LlfSkill = new ArrayList<LightFreelanceSkill>();
		try {
			for (LightFreelanceSkill lskill : llfskill) {
				lskill = createLightFreelanceSkill(lskill);
				LlfSkill.add(lskill);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfSkill;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceSkillDAO#updateLightFreelanceSkill(org.insia.smartposse.metier.webservice.LightFreelanceSkill)
	 */
	public LightFreelanceSkill updateLightFreelanceSkill(LightFreelanceSkill lskill) {
		try {
			FreelanceSkill fskill = DAOFactory.getFreelanceSkillDAO().get(lskill.getId());
			fskill.setFreelance(DAOFactory.getFreelanceDAO().get(lskill.getFreelance()));
			fskill.setPreference(lskill.getPreference());
			fskill.setRate(lskill.getRate());
			fskill.setSkill(DAOFactory.getSkillDAO().get(lskill.getSkill()));
			DAOFactory.getFreelanceSkillDAO().update(fskill);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lskill;
	}
	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelanceSkillDAO#updateLightFreelanceSkill(java.util.List)
	 */
	public List<LightFreelanceSkill> updateLightFreelanceSkill(List<LightFreelanceSkill> llfskill) {
		List<LightFreelanceSkill> LlfSkill = new ArrayList<LightFreelanceSkill>();
		try {
			for (LightFreelanceSkill lskill : llfskill) {
				lskill = updateLightFreelanceSkill(lskill);
				LlfSkill.add(lskill);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfSkill;
	}
}
